const express=require("express");
const path=require("path");
const bodyParser=require("body-parser");
const cookieParser=require("cookie-parser");
const cookieSession=require("cookie-session");
const common=require("./utils/common");
const keys=require("./utils/keys");

const passportRouter=require("./routes/passport");
const webindexRouter=require("./routes/webindex");
const webclassifyRouter=require("./routes/webclassify");
const webseahRouter=require("./routes/webseah");
const webdetailRouter=require("./routes/webdetail");
const webuserRouter=require("./routes/webuser");
const userindexRouter=require("./routes/userindex");

class AppConfig{
    constructor(app){//返回此数组函数的引用
        this.app=app;
        this.listenPort=3000;//设置服务器端口号

        this.app.use(express.static("public"));// 静态资源找寻的文件夹

        this.app.engine('html',require('express-art-template'));//修改模板引擎为html，导入express-art-template
        this.app.set('view options',{
            debugger:process.env.NODE_ENV!=='development'
        });//设置运行的模式为开发模式
        this.app.set('views',path.join(__dirname,'views'));//设置模板存放目录为views文件夹
        this.app.set('view engine','html');//设置引擎后缀为html

        //设置获取请求体,bodyParser功能添加到项目app中
        this.app.use(bodyParser.urlencoded({ extended:false }));// parse application/x-www-form-urlencoded   针对普通页面提交功能
        this.app.use(bodyParser.json());// parse application/json    针对异步提交ajax 

        this.app.use(cookieParser());// cookie的注册
        this.app.use(cookieSession({
            name:"my_session",
            keys:[keys.session_key],
            maxAge:1000*60*60*24*2 //过期时间设置为2天
        }));// session的注册
        // 后面就可以通过 req.session[属性名] = 值来 设置session了

        // 路由的设置
        this.app.use(common.csrfProtect,webindexRouter);//首页相关路由接口
        this.app.use(common.csrfProtect,webclassifyRouter);
        this.app.use(common.csrfProtect,passportRouter);
        this.app.use(common.csrfProtect,webseahRouter);
        this.app.use(common.csrfProtect,webdetailRouter);
        this.app.use(common.csrfProtect,webuserRouter);
        this.app.use(common.csrfProtect,userindexRouter);

        this.app.use((req,res)=>{
            common.abort404(req,res);
        })
    }
}

module.exports=AppConfig;